﻿using System;
using OS.Common.Core;

namespace OS.Common.Plugs.DictionaryPlug
{
    /// <summary>
    /// 字典工具
    /// </summary>
    public static class DictionaryUtil
    {
        private static readonly DefaultDictionaryPlug DefaultCache = new DefaultDictionaryPlug();

        /// <summary>
        /// 通过字典模块名称获取字典模块
        /// </summary>
        /// <param name="dictionaryModule">字典模块名称</param>
        /// <returns></returns>
        private static IDictionaryPlug GetDictionary(string dictionaryModule)
        {
            if (string.IsNullOrEmpty(dictionaryModule))
            {
                dictionaryModule = "default";
            }

            Func<string, IDictionaryPlug> dictionaryProvider = BaseConfig.DictionaryProvider;
            return dictionaryProvider?.Invoke(dictionaryModule) ?? DefaultCache;
        }

        /// <summary>
        /// 设置字典配置信息
        /// </summary>
        /// <param name="key"></param>
        /// <param name="dirConfig"></param>
        /// <param name="moduleName">模块名称</param>
        /// <typeparam name="TConfig"></typeparam>
        /// <returns></returns>
        public static CustomResult SetDictionary<TConfig>(string key, TConfig dirConfig, string moduleName = "default") where TConfig : class, new()
        {
            return GetDictionary(moduleName).SetDictionary(key, dirConfig);
        }

        /// <summary>
        ///   获取字典配置
        /// </summary>
        /// <typeparam name="TConfig"></typeparam>
        /// <param name="key"></param>
        /// <param name="moduleName">模块名称</param>
        /// <returns></returns>
        public static TConfig GetDictionary<TConfig>(string key, string moduleName = "default") where TConfig : class, new()
        {
            return GetDictionary(moduleName).GetDictionary<TConfig>(key);
        }

        /// <summary>
        ///  移除配置信息
        /// </summary>
        /// <param name="key"></param>
        /// <param name="moduleName">模块名称</param>
        /// <returns></returns>
        public static CustomResult RemoveDictionary(string key, string moduleName = "default")
        {
            return GetDictionary(moduleName).RemoveDictionary(key);
        }
    }
}